package com.jxmcloud.business.cut.service.impl.user;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxmcloud.business.cut.dto.client.BuySpeedGiftDto;
import com.jxmcloud.business.cut.dto.client.CouponParamDto;
import com.jxmcloud.business.cut.dtoCommon.CouponDto;
import com.jxmcloud.business.cut.mapper.database.IBackendGiftDBMapper;
import com.jxmcloud.business.cut.mapper.database.IRewardPoolDBMapper;
import com.jxmcloud.business.cut.model.database.DBBackendGift;
import com.jxmcloud.business.cut.model.database.DBGiftBuyRecord;
import com.jxmcloud.business.cut.service.database.IGiftBuyRecordDBService;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.service.impl.logic.CouponHandleImpl;
import com.jxmcloud.business.cut.service.logic.IPayHandle;
import com.jxmcloud.business.cut.service.user.IUserGiftService;
import com.jxmcloud.business.cut.util.SerialNumberUtil;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.xlcloud.platform.api.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserGiftServiceImpl implements IUserGiftService {
    @Autowired
    private IBackendGiftDBMapper backendGiftDBMapper;

    @Autowired
    private IRewardPoolDBMapper rewardPoolDBMapper;

    @Autowired
    private IPayHandle payHandle;

    @Autowired
    private CouponHandleImpl couponHandle;

    @Autowired
    private IGiftBuyRecordDBService giftBuyRecordDBService;

    @Autowired
    private IUserGiftService userGiftService;

    @Autowired
    private IUserActivityInfoDBService userActivityInfoDBService;

    @Override
    public DBGiftBuyRecord getGiftBuyRecord(BuySpeedGiftDto dto, PlayerInfoVo player) {
        DBGiftBuyRecord giftBuyRecord = new DBGiftBuyRecord();
        giftBuyRecord.setUid(player.getBase().getUid());
        giftBuyRecord.setActivityId(player.getActivity().getActivityId());
        giftBuyRecord.setUserActivityInfoId(userActivityInfoDBService.selectByUidAndActivityIdOnDoing(player.getBase().getUid()).getId().intValue());
        giftBuyRecord.setLoginName(player.getBase().getAccount());
        giftBuyRecord.setUsername(player.getBase().getNickName());
        giftBuyRecord.setXlId(player.getBase().getXlId());
        BigDecimal priceBigDecimal = BigDecimal.valueOf(dto.getPayPrice()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        giftBuyRecord.setActualMoney(priceBigDecimal);
        giftBuyRecord.setBuyMoney(priceBigDecimal);
        // 获取礼包优惠券信息
        List<CouponDto> couponDtoList = getGiftCoupon(player);
        giftBuyRecord.setCoupon(JSONUtil.toJsonStr(couponDtoList));
        giftBuyRecord.setOrderNo(SerialNumberUtil.autoOrderNo());
        giftBuyRecord.setCreateTime(new Date());
        return giftBuyRecord;
    }

    @Override
    public boolean buySpeedGift(BuySpeedGiftDto dto, PlayerInfoVo player) {
        // 封装用户购买加速礼包信息
        DBGiftBuyRecord giftBuyRecord = getGiftBuyRecord(dto, player);
        // 平台账号
        Long platAccount = rewardPoolDBMapper.selectOne(null).getXlId();
        // 转账给平台
        boolean flag = payHandle.tansferMoney(player.getBase().getXlId(),platAccount
                , dto.getPayPrice().longValue());
        if(!flag){
            throw new BusinessException("用户转账失败");
        }

        // 发放用户优惠券
        CouponParamDto couponParamDto = new CouponParamDto();
        couponParamDto.setLoginName(player.getBase().getAccount());
        couponParamDto.setOrderNo(giftBuyRecord.getOrderNo());
        couponParamDto.setCouponList(JSONUtil.parseArray(giftBuyRecord.getCoupon()).toList(CouponDto.class));
        couponHandle.sendUserCoupon(couponParamDto);

        // 更新用户加速礼包信息
        return giftBuyRecordDBService.insertIntoData(giftBuyRecord);
    }

    @Override
    public boolean isBuySpeedGift(PlayerInfoVo player) {
        Integer uid = player.getBase().getUid();
        QueryWrapper<DBGiftBuyRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", player.getActivity().getActivityId());
        Long userActivityInfoId = userActivityInfoDBService.selectByUidAndActivityIdOnDoing(player.getBase().getUid()).getId();
        queryWrapper.eq("user_activity_info_id", userActivityInfoId);
        List<DBGiftBuyRecord> dbGiftBuyRecord = giftBuyRecordDBService.getBaseMapper().selectList(queryWrapper);
        if(!dbGiftBuyRecord.isEmpty()){
            return true;
        }
        return false;
    }

    @Override
    public List<CouponDto> getGiftCoupon(PlayerInfoVo player) {
        List<CouponDto> couponDtos = new ArrayList<>();
        Integer activityId = player.getActivity().getActivityId();
        //查询通用礼包数据信息
        QueryWrapper<DBBackendGift> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .eq("activity_id", 0)
                .or()
                .eq("activity_id", activityId)
        );
        List<DBBackendGift> dbBackendGifts = this.backendGiftDBMapper.selectList(queryWrapper);
        for (DBBackendGift gift : dbBackendGifts) {
            CouponDto dto = new CouponDto();
            // 拷贝同名属性
            BeanUtils.copyProperties(gift, dto);
            couponDtos.add(dto);
        }
        return couponDtos;
    }

}
